var uk=window.uk||{};
//将命名空间设置为uk
//元素位置
uk.position = (function(){
  var center = function(html){
    $this = $(html);
    $(html).css({
      'left':'50%',
      'top': '50%',
      'margin-left':$this.outerWidth()/2*-1,
      'margin-top':$this.outerHeight()/2*-1
    })
  }
  return {
    center: center
  }
})()

//显示提示
uk.info = (function(){
    var timeout,alertTimeout,failTimeout;
    var error = function(data){
        clearTimeout(timeout);
        $('.error-info').remove();
        var $dom =$( '<div class="error-info">'+data+'</div>');
        $('body').append($dom);
        uk.position.center('.error-info');
        timeout = setTimeout(function(){
            $('.error-info').remove();
        },2600);
    }
    var alertBox = function(data){
        clearTimeout(alertTimeout);
        $('.alert-box').remove();
        var html = '<div class="alert-box">' + data + '</div>';
        $('body').append(html);
        $('.alert-box').animate({top:'0px'},500);
        alertTimeout = setTimeout(function(){
           $('.alert-box').animate({top:'-50px'},500);
        },1800);
    }
    var alertBox_static = function(data){
        $('.alert-box').remove();
        var html = '<div class="alert-box alert-box_static">' + data + '<i class=" icon-16 white-x " data-modal="close"></i></div>';
        $('body').append(html);
        $('.alert-box').animate({bottom:'-50px'},0);
        alertTimeout = setTimeout(function(){
            $('.alert-box').animate({bottom:'0px'},300);
        },0);
        $('.alert-box_static i').click(function(){
            $(this).parents('.alert-box_static').remove();
        })
    }
    var confirm=function(data,timeNum){
        var timeNum = timeNum || 2600;
        clearTimeout(timeout);
        $('.confirm-info').remove();
        var $dom =$( '<div class="confirm-info">'+data+'</div>');
        $('body').append($dom);
        uk.position.center('.confirm-info');
        timeout = setTimeout(function(){
            $('.confirm-info').remove();
        },timeNum);
    }
    var fail=function(data){
        clearTimeout(failTimeout);
        $('.fail-info').remove();
        var $dom =$( '<div class="fail-info">'+data+'</div>');
        $('body').append($dom);
        uk.position.center('.fail-info');
        failTimeout = setTimeout(function(){
            $('.fail-info').remove();
        },2600);
    }
    return {
        error: error,
        alertBox: alertBox,
        alertBox_static:alertBox_static,
        confirm: confirm,
        fail: fail
    }
})()

uk.modal = (function(){
    var init = function(element){
        if($('.modal-alert.active').length > 1 || $('.modal-confirm.active').length > 1){
            $('.modal-alert,.modal-confirm').not(':last-child').remove();
        }
        if($('[data-ui="modal"]').hasClass('active') && $('.modal_bg').length <1){
            $('body').append('<div class=modal_bg></div>');
        }
        $('body').on('click','[data-modal]',function(){
            var $this = $(this);
            var showElement = $this.data('modal');
            switch(showElement){
                case 'show':
                    uk.modal.show();
                    break;
                case 'close':
                    uk.modal.hide($this);
                    break;
                default:
                    uk.modal.show(showElement);
                    break;
            }
        });
        $('body').on('click','[modal-load]',function(){
            var $this = $(this);
            var url = $this.attr('modal-load');
            var title = $this.attr('modal-title');
            var height = $this.attr('modal-height');
            var width = $this.attr('modal-width');
            var info = $this.attr('modal-info')
            ui.modal.load(url,title,width,height,info);
        });
        uk.position.center('[data-ui="modal"].active');
        $('[data-ui="modal"].active').not('.modal-alert,.modal-confirm').find('.modal_body').height($('[data-ui="modal"].active').attr('modal-height')-105);

    };
    var dom = function(title,width,height,content,button,modal){
        // 是否待确认按钮
        // 是否待取消按钮
        // 高度
        // 宽度 5-12
        // 内容
        var content = content || "载入中...",
            title = title || " ",
            width = width || 6,
            height = height || 'auto';
        button = button || false;

        var html = '<div data-ui="modal" modal-height="'+ height +'" class="column_'+ width +' active modal-'+modal+'"><div class="modal_header"><h4 class="inline on_left">';
        html += title + '</h4><a href="javascript:void(0);" class="on_right icon remove" data-modal="close"></a></div><div class="modal_body">';
        html += content;
        html += '</div>';
        if(button == 'yes,no' || button == 'no,yes'){
            html += '<div class="modal_footer right"><a href="javascript:void(0);" class="button reset  margin-right-10 '+modal+'-no" data-modal="close">取消</a><a href="javascript:void(0);" class="button success '+modal+'-yes" data-modal="close">确定</a></div>';
        }else if(button == 'yes'){
            html += '<div class="modal_footer right"><a href="javascript:void(0);" class="button success '+modal+'-yes" data-modal="close">确定</a></div>';
        }else if(button == 'no'){
            html += '<div class="modal_footer right"><a href="javascript:void(0);" class="button reset'+modal+'-no" data-modal="close">取消</a></div>';
        }else{}

        '</div>';
        return html;
    }
    var show = function(element){
        element = element || false;
        if (!element) {
            // 只有一个弹窗
            $('[data-ui="modal"]').removeClass('active').first().css({'top':$(document).scrollTop()+300,"z-index":200}).addClass('active').find('.modal_body').height($('[data-ui="modal"].active').attr('modal-height')-105);
        } else {
            // 多弹窗
            $('[data-ui="modal"]').removeClass('active');
            $(element).css({'top':$(document).scrollTop()+300,'z-index':200}).addClass('active').find('.modal_body').height($('[data-ui="modal"].active').attr('modal-height')-105);
        };
        // 添加背景
        $('.modal_bg').remove();
        $('body').append('<div class=modal_bg></div>');
        init();
    };
    var hide = function(element){
        $this = element || false;
        if($this){
            $this.parents('[data-ui="modal"]').removeClass('active').css('z-index','-100');
        }else{
            $('[data-ui="modal"].active').not('.modal-alert,.modal-confirm').removeClass('active');
        }
        if($('[data-ui="modal"].active').length < 1) {
            $('.modal_bg').remove();
        }
    };
    var load = function(url,title,width,height,content,button){
        var url = url || false;
        $('.modal-ajax,.modal_bg').remove();
        $('body').append(dom(title,width,height,content,button,'ajax'));
        // 显示弹窗
        if(url){
            $('[data-ui="modal"].active .modal_body').load(url);
        }
        init();
    };
    return {
        _Instance : (function(){
            init();

        })(),
        init : init,
        show : show,
        hide : hide,
        load : load,
        dom : dom
    }
})()
uk.alert = function(content,call){
    $('[data-ui="modal"].modal-alert').remove()
    $('body').append(uk.modal.dom(' ','6','',content,'yes','alert'));
    uk.modal.init();
    var call = call || function(){};
    $('.modal-alert .alert-yes').one('click',function(){
        call();
    })
};
uk.confirm = function(content,call,call2){
    $('[data-ui="modal"].modal-confirm').remove();
    $('body').append(uk.modal.dom(' ','6','',content,'yes,no','confirm'));
    uk.modal.init();
    var call = call || function(){};
    var call2 = call2 || function(){};

    $('.modal-confirm .confirm-yes').one('click',function(callArg){
        if(Object.prototype.toString.call(call)==="[object Array]"){
            call[0](call[1])
        }else{
            call()
        }
    })
    $('.modal-confirm .confirm-no').one('click',function(arg){
        if(Object.prototype.toString.call(call2)==="[object Array]"){
            call2[0](call2[1])
        }else{
            call2()
        }
    });
};


uk.previewImage = function(config){
    var current = arguments[0].current;
    if (!current) return
    $('.previewimage').length > 0
        ? $('.previewimage img').attr('src',current)
        : $('body').append('<div class=previewimage><img src="'+ current +'" /><div class="close2"></div></div>');

    $('.previewimage img').on("load",function(){
        $('.previewimage').addClass('now')
        uk.position.center('.previewimage');
        $('#cover').length > 0
            ? $("#cover").addClass('show')
            : $('body').append('<div id="cover" class="show"></div>')
        $("#cover").on('click',function(){
            $('.previewimage,#cover').removeClass('now show')
        })
    })

}
$(function() {
    // 限制字数
    $('[max-text]').each(function () {
        $($(this).attr('max-for')).html($(this).attr('max-text'));
    })
    var changeInput;
    $('[max-text]').on('input', function () {
        var thisdom = $(this);
        // thisdom.siblings("span").html('正在输入...');
        clearTimeout(changeInput);
        changeInput = setTimeout(function () {
            if (thisdom.attr('max-text') >= thisdom.val().length) {
                // 小于等于限制字数
                $(thisdom.attr('max-for')).html(thisdom.attr('max-text') - thisdom.val().length);
                
            } else {
                // 大于限制字数
                thisdom.val(thisdom.val().substring(0, thisdom.attr('max-text')));
                $(thisdom.attr('max-for')).html(0);
            }
        }, 200);
    });


    $('[tip]').html('?')
    $('body').on('mouseenter', '[tip]', function () {
        $(this).html('?<span class=tip-content>' + $(this).attr('tip') + '</span>').find('.tip-content').css({
            'left': ($(this).offset().left) - ($(this).find('.tip-content').width() / 2) + 8,
            'top': $(this).offset().top - ($(this).find('.tip-content').height()),
        })
    })

    $('.view-num-value').each(function () {
        var $this = $(this)
        setTimeout(function () {
            $this.parents('.view-tiao-out').find('.view-now').animate({'width': $this.text()})
        }, 200)
    })
})

var _qkInput={}
_qkInput._show = function(){};


//判断字符串的字符数
String.prototype.len = function(){ 
   return this.replace(/[^\x00-\xff]/g, "xx").length; 
}